<?php
/**
 * Created by PhpStorm.
 * Author: Shadow
 * Date: 2022/5/20
 * Time: 2:40 下午
 * description: BrandBusiness.php
 */

namespace app\adminapi\business;

/**
 * 品牌的业务逻辑类
 * 通过业务逻辑层进行处理
 * Class BrandBusiness
 * @package app\adminapi\business
 */
class BrandBusiness
{

    /**
     * 查询品牌的列表
     * @param $limit
     * @return \app\common\model\Brand|string|\think\Paginator
     */
    public static function list($limit){

        # 多条件搜索
        $where = [];
        $name = request()->get('name');
        if (!empty($name)){
            $where[] = ['name','like',"$name%"];
        }
        $cate_id = request()->get('cate_id');
        if (!empty($cate_id) && !is_numeric($cate_id)) {
            abort(2001,'分类id只能是数字');
        }elseif ($cate_id > 0) {
            $where[] = ['cate_id','=',$cate_id];
        }
        $data = \app\common\model\Brand::with('category')->where($where)->paginate($limit);

        return $data;
    }


    /**
     * 删除品牌
     * @param $id
     * @return bool
     */
    public static function delete($id){
        // 品牌下面有上架的商品不能删除 属于逻辑

        # 调用商品模型、判断商品
        $isExistGoods = \app\common\model\Goods::where('is_on_sale','=',1)->where('brand_id','=',$id)->select()->toArray();

        if (!empty($isExistGoods)) {
            abort(4003,'该品牌下面有上架商品、不能删除');
        }
        # 调用品牌模型、进行删除
        $result = \app\common\model\Brand::destroy($id);
        if (!$result) {
            abort(4002,'删除失败');
        }
        return true;

    }

}